package com.digitalchina.lxnsy.toolkit;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedPreferencesUtil {

	public static String getValue(Context poContext, String psName,
								  String psKey, String defaultValue) {
		if (poContext == null) {
			return defaultValue;
		}
		SharedPreferences loCfg = null;
		loCfg = poContext.getSharedPreferences(psName, 0);
		return loCfg.getString(psKey, defaultValue);
	}

	public static void setValue(Context poContext, String psName, String psKey,
								String psValue) {
		if (poContext == null)
			return;
		Editor loEditor = null;
		SharedPreferences loCfg = poContext.getSharedPreferences(psName, 0);
		loEditor = loCfg.edit();
		if (psValue != null) {
			loEditor.putString(psKey, psValue);

		} else {
			loEditor.remove(psKey);
		}
		loEditor.apply();
	}

	public static void cleanXml(Context poContext, String psName) {
		SharedPreferences loCfg = poContext.getSharedPreferences(psName, 0);
		Editor loEditor = loCfg.edit();
		loEditor.clear();
		loEditor.apply();
	}

	public static void removeKey(Context poContext, String psName, String psKey) {
		SharedPreferences loCfg = poContext.getSharedPreferences(psName, 0);
		Editor loEditor = loCfg.edit();
		loEditor.remove(psKey);
		loEditor.apply();
	}

	/**
	 * 查看xml是否包含psKey
	 * 
	 * @param poContext
	 * @param psName
	 * @param psKey
	 * @return
	 */
	public static boolean isContains(Context poContext, String psName,
									 String psKey) {
		if (poContext == null) {
			return false;
		}
		SharedPreferences loCfg = null;
		loCfg = poContext.getSharedPreferences(psName, 0);
		return loCfg.contains(psKey);
	}

	public static void cleanAllXml(Context poContext) {

	}
}
